from rest_framework.views import APIView
from rest_framework.generics import DestroyAPIView
from CeleryTimedTask.HttpTestcase.testsuiteset_task import batch_exec_testsuiteset
from HttpTestcase.core.utils import get_version
from utils.viewsets import ModelViewSet
from apps.HttpTestcase.models.testsuite_set import TestsuiteSet
from apps.HttpTestcase.serializers.testsuite_set import TestsuiteSetListModeSerializer, TestsuiteSetModeSerializer
from apps.HttpTestcase.filters.testsuite_set import TestsuiteSetFilters
from utils.viewsets import *


class TestSuiteList(ReadOnlyModelViewSet):
    queryset = TestsuiteSet.objects.filter(is_delete=False)
    serializer_class = TestsuiteSetListModeSerializer
    filter_class = TestsuiteSetFilters


class TestSuiteSetModelViewSet(CreateModelMixin, UpdateModelMixin, GenericViewSet, DestroyModelMixin):
    queryset = TestsuiteSet.objects.filter(is_delete=False)
    serializer_class = TestsuiteSetModeSerializer


class TestsuiteSetBatchAPIView(APIView):
    def post(self, request, *args, **kwargs):
        pass
        data = request.data
        testsuites_set_list = data.get('testsuites_set')
        version = get_version()
        batch_exec_testsuiteset(testsuites_set_list=testsuites_set_list, version=version)
        return SuccessResponse(data=None, msg='程序正在后台运行中,请稍后查看结果……')


class TestsuiteSetBatchDelete(DestroyAPIView):
    def delete(self, request, *args, **kwargs):
        testsuite_set_id = request.data
        TestsuiteSet.objects.filter(id__in=testsuite_set_id).update(is_delete=True)
        return SuccessResponse(data=None, msg='删除成功！')


